package com.cmccgz.udacs_landlord_webapp.main.peoplemanager.presenter;

import android.content.Context;

import com.cmccgz.baselibrary.utils.NetUtils;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.base.ApiService;
import com.cmccgz.udacs_landlord_webapp.base.GlobalContext;
import com.cmccgz.udacs_landlord_webapp.bean.CardDetail;
import com.cmccgz.udacs_landlord_webapp.bean.CardMsg;
import com.cmccgz.udacs_landlord_webapp.bean.ResultBean;
import com.cmccgz.udacs_landlord_webapp.main.peoplemanager.contract.StopContract;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static com.cmccgz.udacs_landlord_webapp.main.peoplemanager.XiaoCardActivity.CARD_NEGATIVE;

/**
 * @author Denis
 * @ClassName: DelayPresenter
 * @Description: 暂停租客房卡
 * @date 2017/6/7 14:37
 */

public class StopPresenter implements StopContract.Presenter {
    private StopContract.View view;

    public StopPresenter(StopContract.View view) {
        this.view = view;
        view.setPresenter(this);
    }

    @Override
    public void stopCard(String user_mobile, String house_id, List<CardDetail> cards) {
        Context context = GlobalContext.getInstance();
        if (!NetUtils.isConnected(context)) {
            view.stopCardFail(context.getString(R.string.noNetWork));
            return;
        }
        List<CardDetail>stopCards=new ArrayList<>();
        //截取需要恢复的卡片
        for (int i = 0;cards!=null&&i < cards.size(); i++) {
            CardDetail card = cards.get(i);
            try {
                if (CARD_NEGATIVE.equals(card.card_state)) {
                    stopCards.add(card);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (stopCards.size()==0) {
            view.stopCardFail("暂无需要暂停的卡片！");
            return;
        }
        ApiService.changeCardState(user_mobile,house_id,stopCards)
                .subscribe(new Subscriber<ResultBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        view.stopCardFail("暂停卡片失败，请稍后再试！");
                    }

                    @Override
                    public void onNext(ResultBean resultBean) {
                        if ("0".equals(resultBean.rsmsg.rscode)) {
                            view.stopCardSuccess();
                        }else{
                            view.stopCardFail(resultBean.rsmsg.rsdesc);
                        }
                    }
                });
    }

    @Override
    public void getActiveCardMsg(String user_mobile, String house_id) {
        Context context = GlobalContext.getInstance();
        if (!NetUtils.isConnected(context)) {
            view.getCardMsgFail(context.getString(R.string.noNetWork));
            return;
        }
        ApiService.getCardDetail(user_mobile, house_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResultBean<CardMsg>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        view.getCardMsgFail("获取卡片信息失败，请稍后再试！");
                    }

                    @Override
                    public void onNext(ResultBean<CardMsg> cardMsgResultBean) {
                        if ("0".equals(cardMsgResultBean.rsmsg.rscode)) {
                            ArrayList<CardDetail> card_details = cardMsgResultBean.resp.card_detail;
                            if (card_details==null) {
                                card_details=new ArrayList<CardDetail>();
                                cardMsgResultBean.resp.card_detail=card_details;
                            }
                            Iterator<CardDetail> iterator = card_details.iterator();
                            while (iterator.hasNext()){
                                CardDetail cardDetail = iterator.next();
                                if (CARD_NEGATIVE.equals(cardDetail.card_state)) {
                                    iterator.remove();
                                }
                            }
                            view.getCardMsgSuccess(cardMsgResultBean.resp);
                        }else{
                            view.getCardMsgFail(cardMsgResultBean.rsmsg.rsdesc);
                        }
                    }
                });
    }

    @Override
    public void start() {

    }
}
